<?php

namespace app\Backend\controller;

use think\Controller;
use think\Request;
use app\common\model\Post as PostModel;
use app\common\model\Tag as TagModel;

class Post extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $posts = PostModel::order('create_time', 'desc')->paginate(10);
        return view('', compact('posts'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $tags = TagModel::all();
        return view('', compact('tags'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $tags = $request->post('tags/a');
        $title = $request->post('title');
        $content = $request->post('content');
        if ($title == '' || $content == '') {
            $this->error('请输入完整内容');
        }
        $post = new PostModel;
        $post->title = $title;
        $post->content = $content;
        if (!$post->save()) {
            $this->error('创建贴子失败，请稍后再试');
        }
        //需要关联标签
        $tags && $post->tags()->attach($tags);
        $this->success('创建成功！');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $post = PostModel::find($id);
        !$post && $this->error('贴子不存在！');
        $tags = TagModel::all();
        return view('', compact('tags', 'post'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $tags = $request->post('tags/a');
        $title = $request->post('title');
        $content = $request->post('content');
        if ($title == '' || $content == '') {
            $this->error('请输入完整内容');
        }
        $post = PostModel::find($id);
        $post->title = $title;
        $post->content = $content;
        if (!$post->save()) {
            $this->error('保存贴子失败，请稍后再试');
        }
        //需要关联标签
        $postTags = $post->tags;
        if ($postTags) {
            // 存在关联标签
            $postTagsIds = array_column($postTags->toArray(), 'id');
            // 删除这些已有的关联的标签
            $post->tags()->detach($postTagsIds);
        }
        $tags && $post->tags()->saveAll($tags);
        $this->success('创建成功！');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $post = PostModel::find($id);
        if (!$post) {
            $this->error('贴子不存在！');
        }

        //取消标签的关联
        $tags = $post->tags;
        if ($tags) {
            $tagIds = array_column($tags->toArray(), 'id');
            $post->tags()->detach($tagIds);
        }

        if (!$post->delete()) {
            $this->error('贴子删除失败!');
        }

        $this->success('贴子删除成功！');

    }
}
